#include"apue.h"
/*
#ifndef DEFINE_H
#define DEFINE_H
#define CMD_LS_CLIENT 11
#define CMD_LS_SERVE 22
#define CMD_GET 33
#define CMD_PUT 44
#define CMD_QUIT 55
#define CMD_ERROR -1

#define E_NOFILE    "ERROR:No such file or directory!\n"
#define E_GET 		 "ERROR:Download error!\n"
#define E_PUT   	 "ERROR:Upload error!\n"
#define GET_LIST_END "SUCCESS:GET LIST SUCCESS!"
*/
//命令解析函数
int ftp_cmd_analyse(const char* cmd)
{
	if(cmd==NULL)
	{
		return CMD_ERROR;
	}
	if(strcmp(cmd,"ls")==0)
	{
		return CMD_LS;
	}
	else if(strncmp(cmd,"cd",2)==0)
	{
		return CMD_CD;
	}
	else if(strncmp(cmd,"bye",3)==0)
	{
		return CMD_QUIT;
	}
	else if(strcmp(cmd,"bye")==0)
	{
		return CMD_QUIT;
	}
	else if(strncmp(cmd,"get",3)==0)
	{
		return CMD_GET;
	}
	else if(strncmp(cmd,"put",3)==0)
	{
		return CMD_PUT;
	}
	else 
	{
		return CMD_ERROR;
	}
}
int pasv(int sockfd)
{
	char buf[255]={};
	int port;
	send(sockfd,"PASV\n",5,0);
	recv(sockfd,buf,sizeof(buf),0);
	int i=0;
	char str[255];
	int pos =0;
	//printf("---------------\n");
	while(buf[i])
	{
		i++;
		if('('==buf[i])
		{
			i++;
			while(')'!=buf[i])
			{
				str[pos++]=buf[i++];
			}
		}

	}
	
	str[pos]='\0';
	char sign[]=",";
	char* p=NULL;
	i=0;
	int arr[6]={};
	p=strtok(str,sign);
	while(NULL!=p)
	{
		arr[i]=atoi(p);
		p=strtok(NULL,sign);
		i++;
	}
	
	port=arr[4]*256+arr[5];
	return port;

}
char* get_time(char* filename,int filesize,char* buf,int len)
{
	char time[64];
	strncpy(time,buf+4,strlen(buf)+1-4);

	char year[5] = {};
	strncpy(year,time,4);

	char month[3]= {};
	strncpy(month,time+4,2);

	char day[3]= {};
	strncpy(day,time+6,2);

	char hour[3]= {};
	strncpy(hour,time+8,2);

	char min[3]= {};
	strncpy(min,time+10,2);

	char sec[3]= {};
	strncpy(sec,time+12,2);

	struct tm at;

	at.tm_year =  atoi(year)-1900;
	at.tm_mon =  atoi(month)-1;
	at.tm_mday = atoi(day);
	at.tm_hour =  atoi(hour);
	at.tm_min =  atoi(min);
	at.tm_sec =  atoi(sec);

	printf("%d %d %d\n",at.tm_year,at.tm_mon,at.tm_mday);
	time_t newt = mktime(&at);
	struct utimbuf new = {newt,newt};
	utime(filename,&new);

}
